/* eslint-disable no-undef */
import { DeviceInfo } from './DeviceInfo'
const LOG_EVENT = '/log-trace/user/event'

export const Statics = (function() {
  /**
   * 构造device设备参数结构体
   */
  const buildDeviceParam = function(callback) {
    DeviceInfo.getDeviceInfo(
      {
        info: ['date', 'UUID', 'OS', 'deviceType', 'screenWidth', 'screenHeight', 'browserInfo', 'fingerprint', 'userAgent', 'language', 'OSVersion'],
      }, function(deviceinfo) {
        const result = {}
        result.curTime = parseInt(new Date().getTime() / 1000)
        result.beginTime = parseInt(new Date().getTime() / 1000)

        let uuid = uni.getStorageSync('UUID')
        if (!uuid) {
          uuid = deviceinfo.UUID
          uni.setStorageSync('UUID', deviceinfo.UUID)
        }
        // 设备id
        result.deviceId = uuid
        result.userId = getApp().globalData.userNo || uuid

        // 平台
        result.platform = deviceinfo.OS
        // 设备类型
        result.deviceType = deviceinfo.deviceType
        // 水平像素
        result.lev = deviceinfo.screenWidth
        // 垂直像素
        result.hig = deviceinfo.screenHeight
        // 设备品牌
        result.deviceBrand = deviceinfo.browserInfo
        // 设备型号
        result.deviceModel = deviceinfo.fingerprint
        // mac地址
        result.mac = deviceinfo.userAgent
        // 语言
        result.language = deviceinfo.language
        // 浏览器版本信息
        result.ov = deviceinfo.OSVersion

        // platform
        result.platform = 3
        // appId,随便整的一个
        result.appId = 1

        callback && callback(result)
      },
    )
  }

  return {
    buildDeviceParam(callback) {
      buildDeviceParam(callback)
    },
    onEvent(params) {
      this.buildDeviceParam((data) => {
        let allParams = { ...data, ...params }
        allParams.channel = 'h5'
        /*
         * 小程序的取数逻辑不同
         */
        // #ifdef MP-WEIXIN
        const res = uni.getSystemInfoSync()
        // 'iPhone XS MAX China-exclusive<iPhone 11,6>'
        if (res.SDKVersion === '2.18.0' && res.model.toLocaleLowerCase().includes('iphone xs max')) {
          return
        }

        allParams = { ...allParams, ...res }
        allParams.platform = 3
        allParams.channel = 'MiniProgram'
        // #endif

        const submitUrl = getApp().globalData.logHost + LOG_EVENT
        uni.request({
          url: submitUrl,
          data: allParams,
          header: {
            'Content-Type': 'application/json',
          },
        }).then(() => {

        }, (err) => {
          console.warn(123 + JSON.stringify(err))
        })
      })
    },
  }
})()
